home *** CD-ROM | disk | FTP | other *** search
/ Internet.Works 40 / Issue 40.iso / pc / PCSoftware / DHTML Menu Builder 2.6 / dhtmlmen.exe / %AppDir% / AddIns / AutoCloseTimer.ext next >
Encoding:
Text File  |  2000-10-26  |  16.1 KB  |  583 lines

  1. This AddIn will automaticlly close any opened menus after a defined period of time if the mouse is not over any menu.
  2. ***
  3. //COPYRIGHT
  4.     
  5.     var nStyle = new Array;
  6.     var hStyle = new Array;
  7.     var nLayer = new Array;
  8.     var hLayer = new Array;
  9.     var nTCode = new Array;
  10.     
  11.     var AnimStep = 0;
  12.     var AnimHnd = 0;
  13.     var HTHnd = new Array;
  14.     var DoFormsTweak = false;
  15.     
  16.     var mFrame;
  17.     var cFrame;
  18.     
  19.     var OpenMenus = new Array;
  20.     var SelCommand;
  21.     var nOM = 0;
  22.     
  23.     var mX;
  24.     var mY;
  25.     var xOff = 0;
  26.     
  27.     var HideSpeed = 200;
  28.     
  29.     var TimerHideDelay = 2000;
  30.     var TimerHideHnd = 0;
  31.     
  32. //BROWSERDETECTION
  33.     
  34.     if ((frames.length==0) && IsMac)
  35.     frames.top = window;
  36.     
  37.     if(IE)
  38.     xOff = 2;
  39. //CODEFRAME
  40.     
  41. //FX
  42.     
  43. //STYLECODE
  44.  
  45.     function GetCurCmd() {
  46.         //IE
  47.         //This function will return the current command under the mouse pointer.
  48.         //It will return null if the mouse is not over any command.
  49.         //------------------------------
  50.         //Version 1.3
  51.         //
  52.         var cc = mFrame.window.event.srcElement;
  53.         while((cc.id=="") && (cc.tagName!="TD")) {
  54.             cc = cc.parentElement;
  55.             if(cc==null)
  56.                 break;
  57.         }
  58.         return cc;
  59.     }
  60.  
  61.     function HoverSel(mode, imgLName, imgRName) {
  62.         //IE
  63.         //This is the function called every time the mouse pointer is moved over a command.
  64.         //------------------------------
  65.         //mode: 0 if the mouse is moving over the command and 1 if is moving away.
  66.         //imgLName: Name of the left image object, if any.
  67.         //imgRName: Name of the right image object, if any.
  68.         //------------------------------
  69.         //Version 9.3
  70.         //
  71.         var imgL = new Image;
  72.         var imgLRsc = new Image;
  73.         var imgR = new Image;
  74.         var imgRRsc = new Image;
  75.         var mc;
  76.         
  77.         if(mode==0 && OpenMenus[nOM].SelCommand!=null)
  78.             HoverSel(1);
  79.         
  80.         if(imgLName!="_")
  81.             var imgL = eval("mFrame.document.images['"+imgLName+"']");
  82.         if(imgRName!="_")
  83.             var imgR = eval("mFrame.document.images['"+imgRName+"']");
  84.         
  85.         if(mode==0) {
  86.             mc = GetCurCmd();
  87.             if(nOM>1)
  88.                 if(mc==OpenMenus[nOM-1].SelCommand)
  89.                     return false;
  90.             if(OpenMenus[nOM].SelCommand || nOM>1)
  91.                 while(!InMenu())
  92.                     Hide();
  93.             mc.style.cssText = hStyle[mc.id];
  94.             if(imgLName!='_') imgLRsc = eval(imgLName+"On");
  95.             if(imgRName!='_') imgRRsc = eval(imgRName+"On");
  96.             OpenMenus[nOM].SelCommand = mc;
  97.             OpenMenus[nOM].SelCommandPar = [imgLName,imgRName];
  98.         } else {
  99.             mc = (mode==1)?OpenMenus[nOM].SelCommand:OpenMenus[nOM].Opener;
  100.             imgLName = (mode==1)?OpenMenus[nOM].SelCommandPar[0]:OpenMenus[nOM].OpenerPar[0];
  101.             imgRName = (mode==1)?OpenMenus[nOM].SelCommandPar[1]:OpenMenus[nOM].OpenerPar[1];
  102.             mc.style.background = "";
  103.             mc.style.cssText = nStyle[mc.id];
  104.             if(imgLName!='_') imgLRsc = eval(imgLName+"Off");
  105.             if(imgRName!='_') imgRRsc = eval(imgRName+"Off");
  106.             window.status = "";
  107.             OpenMenus[nOM].SelCommand = null;            
  108.         }
  109.         
  110.         if(imgLName!='_') {
  111.             imgL = eval("mFrame.document.images."+imgLName);
  112.             imgL.src = imgLRsc.src;
  113.         }
  114.         if(imgRName!='_') {
  115.             imgR = eval("mFrame.document.images."+imgRName);
  116.             imgR.src = imgRRsc.src;
  117.         }
  118.         
  119.         return true;
  120.     }
  121.  
  122.     function NSHoverSel(mode, mc, bcolor, w, h) {
  123.         //NS
  124.         //This is the function called every time the mouse pointer is moved over or away from a command.
  125.         //------------------------------
  126.         //mode: 0 if the mouse is moving over the command and 1 if is moving away.
  127.         //mc: Name of the layer that corresponds to the selected command.
  128.         //n: Unique ID that identifies this command. Used to retrieve the data from the nLayer or hLayer array.
  129.         //bcolor: Background color of the command. Ignored if the group uses a background image.
  130.         //w: Width of the command's layer.
  131.         //h: Height of the command's layer.
  132.         //------------------------------
  133.         //Version 10.5
  134.         //
  135.         var n;
  136.         var LayerHTM;
  137.         if(mode==0 && OpenMenus[nOM].SelCommand!=null)
  138.             NSHoverSel(1);
  139.         
  140.         if(mode==0) {
  141.             n = CBparseInt(mc.name.substr(2));
  142.             if(nOM>1)
  143.                 if(mc==OpenMenus[nOM-1].SelCommand)
  144.                     return false;
  145.             while(!InMenu())
  146.                 Hide();
  147.             LayerHTM = hLayer[n];
  148.             OpenMenus[nOM].SelCommand = mc;
  149.             OpenMenus[nOM].SelCommandPar = [mc.bgColor,w,h];
  150.             mc.bgColor = bcolor;
  151.         } else {
  152.             mc = (mode==1)?OpenMenus[nOM].SelCommand:OpenMenus[nOM].Opener;
  153.             bcolor = (mode==1)?OpenMenus[nOM].SelCommandPar[0]:OpenMenus[nOM].OpenerPar[0];
  154.             w = (mode==1)?OpenMenus[nOM].SelCommandPar[1]:OpenMenus[nOM].OpenerPar[1];
  155.             h = (mode==1)?OpenMenus[nOM].SelCommandPar[2]:OpenMenus[nOM].OpenerPar[2];
  156.             n = CBparseInt(mc.name.substr(2));
  157.             LayerHTM = nLayer[n];
  158.             if(mc.parentLayer.background.src!="")
  159.                 mc.bgColor = null;
  160.             else
  161.                 mc.bgColor = bcolor;
  162.             window.status = "";
  163.             OpenMenus[nOM].SelCommand = null;
  164.         }
  165.         mc.resizeTo(w,h);
  166.         mc.document.open();
  167.         mc.document.write(LayerHTM);
  168.         mc.document.close();
  169.         
  170.         return true;
  171.     }
  172.  
  173.     function Hide() {
  174.         //IE,NS
  175.         //This function hides the last opened group and it keeps hiding all the groups until
  176.         //no more groups are opened or the mouse is over one of them.
  177.         //Also takes care of reseting any highlighted commands.
  178.         //------------------------------
  179.         //Version 3.2
  180.         //
  181.         
  182.         if(AnimHnd)
  183.             window.clearTimeout(AnimHnd);
  184.         
  185.         if(OpenMenus[nOM].SelCommand!=null) {
  186.             if(IE) HoverSel(1);
  187.             if(NS) NSHoverSel(1);
  188.         }
  189.         if(OpenMenus[nOM].Opener!=null) {
  190.             if(IE) HoverSel(3);
  191.             if(NS) NSHoverSel(3);
  192.         }
  193.         
  194.         OpenMenus[nOM].visibility = "hidden";
  195.         window.clearTimeout(HTHnd[nOM]);
  196.         HTHnd[nOM] = 0;
  197.         nOM--;
  198.         
  199.         if(nOM>0)
  200.             if(!InMenu())
  201.                 HTHnd[nOM] = window.setTimeout("Hide()", HideSpeed);
  202.         
  203.         if(nOM==0)
  204.             FormsTweak("visible");
  205.     }
  206.  
  207.     function ShowMenu(mName, x, y, isCascading) {
  208.         //IE,NS
  209.         //This is the main function to show the menus when a hotspot is triggered or a cascading command is activated.
  210.         //------------------------------
  211.         //mName: Name of the <div> or <layer> to be shown.
  212.         //x: Left position of the menu.
  213.         //y: Top position of the menu.
  214.         //isCascading: True if the menu has been triggered from a command, and not from a hotspot.
  215.         //------------------------------
  216.         //Version 14.0
  217.         //
  218.         x = CBparseInt(x);
  219.         y = CBparseInt(y);
  220.         if(AnimHnd && nOM>0) {
  221.             AnimStep=100;
  222.             Animate();
  223.         }
  224.         if(IE)
  225.             var Menu = mFrame.document.all[mName];
  226.         if(NS)
  227.             var Menu = mFrame.document.layers[mName];
  228.         if(!Menu)
  229.             return false;
  230.         if(IE)
  231.             Menu = Menu.style;
  232.         if(Menu==OpenMenus[nOM] || HTHnd[nOM])
  233.             return false;
  234.         
  235.         if(TimerHideHnd) {
  236.             window.clearTimeout(TimerHideHnd);
  237.             TimerHideHnd = 0;
  238.         }
  239.         
  240.         Menu.Opener = nOM>0?OpenMenus[nOM].SelCommand:null;
  241.         Menu.OpenerPar = nOM>0?OpenMenus[nOM].SelCommandPar:null;
  242.         Menu.SelCommand = null;
  243.         
  244.         if(!isCascading)
  245.             HideAll();
  246.         
  247.         var pW = GetWidthHeight()[0] + GetLeftTop()[0];
  248.         var pH = GetWidthHeight()[1] + GetLeftTop()[1];
  249.         
  250.         if(IE) {
  251.             if(isCascading) {
  252.                 x = CBparseInt(OpenMenus[nOM].left) + CBparseInt(OpenMenus[nOM].width) - 6;
  253.                 y = y + CBparseInt(OpenMenus[nOM].top) - 5;
  254.                 Menu.left = (x+CBparseInt(Menu.width)>pW)?CBparseInt(OpenMenus[nOM].left) - CBparseInt(Menu.width) + 6:x;
  255.                 Menu.top =  (y+CBparseInt(Menu.height)>pH)?pH - CBparseInt(Menu.height):y;
  256.             } else {
  257.                 Menu.left = (x+CBparseInt(Menu.width)>pW)?pW - CBparseInt(Menu.width):x;
  258.                 Menu.top =  (y+CBparseInt(Menu.height)>pH)?pH - CBparseInt(Menu.height):y;
  259.             }
  260.             if(!IsMac)
  261.                 Menu.clip = "rect(0 0 0 0)";
  262.         }
  263.         if(NS) {
  264.             if(isCascading) {
  265.                 x = OpenMenus[nOM].left + OpenMenus[nOM].clip.width - 6;
  266.                 y = OpenMenus[nOM].top + OpenMenus[nOM].SelCommand.top;
  267.                 x = (x+Menu.w>pW)?OpenMenus[nOM].left - Menu.w + 6:x;
  268.                 y = (y+Menu.h>pH)?pH - Menu.h:y;
  269.             } else {
  270.                 x = (x+Menu.w>pW)?pW - Menu.w:x;
  271.                 y = (y+Menu.h>pH)?pH - Menu.h:y;
  272.             }
  273.             Menu.clip.width = 0;
  274.             Menu.clip.height = 0;
  275.             Menu.moveToAbsolute(x,y);
  276.         }
  277.         if(isCascading)
  278.             Menu.zIndex = CBparseInt(OpenMenus[nOM].zIndex) + 1;
  279.         Menu.visibility = "visible";
  280.         OpenMenus[++nOM] = Menu;
  281.         HTHnd[nOM] = 0;
  282.         if((IE && !IsMac) || NS)
  283.             AnimHnd = window.setTimeout("Animate()", 10);
  284.         FormsTweak("hidden");
  285.         
  286.         TimerHideHnd = window.setTimeout("AutoHide()", TimerHideDelay);
  287.         
  288.         return true;
  289.     }
  290.  
  291.     function Animate() {
  292.         //IE,NS
  293.         //This function is called by ShowMenu every time a new group must be displayed and produces the predefined unfolding effect.
  294.         //Currently is disabled for Navigator, because of some weird bugs we found with the clip property of the layers.
  295.         //------------------------------
  296.         //Version 1.9
  297.         //
  298.         var r = '';
  299.         var nw = nh = 0;
  300.         switch(fx) {
  301.             case 1:
  302.                 if(IE) r = "0 " + AnimStep + "% " + AnimStep + "% 0";
  303.                 if(NS) nw = AnimStep; nh = AnimStep;
  304.                 break;
  305.             case 2:
  306.                 if(IE) r = "0 100% " + AnimStep + "% 0";
  307.                 if(NS) nw = 100; nh = AnimStep;
  308.                 break;
  309.             case 3:
  310.                 if(IE) r = "0 " + AnimStep + "% 100% 0";
  311.                 if(NS) nw = AnimStep; nh = 100;
  312.                 break;
  313.             case 0:
  314.                 if(IE) r = "0 100% 100% 0";
  315.                 if(NS) nw = 100; nh = 100;
  316.                 break;
  317.         }
  318.         with(OpenMenus[nOM]) {
  319.             if(IE)
  320.                 clip =  "rect(" + r + ")";
  321.             if(NS) {
  322.                 clip.width = w*(nw/100);
  323.                 clip.height = h*(nh/100);
  324.             }
  325.         }
  326.         AnimStep += 20;
  327.         if(AnimStep<=100)
  328.             AnimHnd = window.setTimeout("Animate()",25);
  329.         else {
  330.             window.clearTimeout(AnimHnd);
  331.             AnimStep = 0;
  332.             AnimHnd = 0;
  333.         }
  334.     }
  335.  
  336.     function InMenu() {
  337.         //IE,NS
  338.         //This function returns true if the mouse pointer is over the last opened menu.
  339.         //------------------------------
  340.         //Version 1.6
  341.         //
  342.         var m = OpenMenus[nOM];
  343.         if(!m)
  344.             return false;
  345.         if(IE&&BV==4)
  346.             SetPointerPos();
  347.         var l = CBparseInt(m.left) + xOff;
  348.         var r = l+((IE)?CBparseInt(m.width):m.clip.width) - xOff;
  349.         var t = CBparseInt(m.top) + xOff;
  350.         var b = t+((IE)?CBparseInt(m.height):m.clip.height) - xOff;
  351.         return ((mX>=l && mX<=r) && (mY>=t && mY<=b));
  352.     }
  353.  
  354.     function SetPointerPos(e) {
  355.         //IE,NS
  356.         //This function sets the mX and mY variables with the current position of the mouse pointer.
  357.         //------------------------------
  358.         //e: Only used under Navigator, corresponds to the Event object.
  359.         //------------------------------
  360.         //Version 1.0
  361.         //
  362.         if(IE) {
  363.             if(event==null)
  364.                 if(mFrame.window.event==null)
  365.                     return;
  366.                 else
  367.                     e = mFrame.window.event;
  368.             else
  369.                 e = event;
  370.             mX = e.clientX + mFrame.document.body.scrollLeft;
  371.             mY = e.clientY + mFrame.document.body.scrollTop;
  372.         }
  373.         if(NS) {
  374.             mX = e.pageX;
  375.             mY = e.pageY;
  376.         }
  377.     }
  378.  
  379.     function HideMenus(e) {
  380.         //IE,NS
  381.         //This function checks if the mouse pointer is on a valid position and if the current menu should be kept visible.
  382.         //The function is called every time the mouse pointer is moved over the document area.
  383.         //------------------------------
  384.         //e: Only used under Navigator, corresponds to the Event object.
  385.         //------------------------------
  386.         //Version 24.3
  387.         //
  388.         SetPointerPos(e);
  389.         if(nOM>0)
  390.             if(OpenMenus[nOM].SelCommand!=null)
  391.                 while(!InMenu() && !HTHnd[nOM]) {
  392.                     HTHnd[nOM] = window.setTimeout("Hide()", HideSpeed);
  393.                     if(nOM==0)
  394.                         break;
  395.                 }
  396.     }
  397.  
  398.     function FormsTweak(state) {
  399.         //IE
  400.         //This is an undocumented function, which can be used to hide every listbox (or combo) element on a page.
  401.         //This can be useful if the menus will be displayed over an area where is a combo box, which is an element that cannot be placed behind the menus and it will always appear over the menus resulting in a very undesirable effect.
  402.         //------------------------------
  403.         //Version 2.0
  404.         //
  405.         if(DoFormsTweak && IE)
  406.             for(var f = 0; f <= (mFrame.document.forms.length - 1); f++)
  407.                 for(var e = 0; e <= (mFrame.document.forms[f].elements.length - 1); e++)
  408.                     if(mFrame.document.forms[f].elements[e].type=="select-one")
  409.                         mFrame.document.forms[f].elements[e].style.visibility = state;
  410.     }
  411.  
  412.     function execURL(url, tframe) {
  413.         //IE,NS
  414.         //This function is called every time a command is triggered to jump to another page or execute some javascript code.
  415.         //------------------------------
  416.         //url: Encrypted URL that must be opened or executed.
  417.         //tframe: If the url is a document location, tframe is the target frame where this document will be opened.
  418.         //------------------------------
  419.         //Version 1.1
  420.         //
  421.         HideAll();
  422.         window.setTimeout("execURL2('" + url + "', '" + tframe + "')", 100);
  423.     }
  424.  
  425.     function execURL2(url, tframe) {
  426.         //IE,NS
  427.         //This function is called every time a command is triggered to jump to another page or execute some javascript code.
  428.         //------------------------------
  429.         //url: Encrypted URL that must be opened or executed.
  430.         //tframe: If the url is a document location, tframe is the target frame where this document will be opened.
  431.         //------------------------------
  432.         //Version 1.0
  433.         //
  434.         tframe = rStr(tframe);
  435.         var fObj = eval(tframe);
  436.         url = rStr(url);
  437.         if(url.indexOf("javascript")!=url.indexOf("vbscript"))
  438.             eval(url);
  439.         else
  440.             fObj.location.href = url;
  441.     }
  442.  
  443.     function rStr(s) {
  444.         //IE,NS
  445.         //This function is used to decrypt the URL parameter from the triggered command.
  446.         //------------------------------
  447.         //Version 1.1
  448.         //
  449.         s = xrep(s,"\x1E","'");
  450.         s = xrep(s,"\x1D","\x22");
  451.         s = xrep(s,"\x1C",",");
  452.         return s;
  453.     }
  454.  
  455.     function xrep(s, f, n) {
  456.         //IE,NS
  457.         //This function looks for any occurrence of the f string and replaces it with the n string.
  458.         //------------------------------
  459.         //Version 1.0
  460.         //
  461.         var tmp = s.split(f);
  462.         return tmp.join(n);
  463.     }
  464.  
  465.     function hNSCClick(e) {
  466.         //NS
  467.         //This function executes the selected command's trigger code.
  468.         //------------------------------
  469.         //Version 1.0
  470.         //
  471.         eval(this.TCode);
  472.     }
  473.     
  474.     function CBparseInt(n) {
  475.         //NS
  476.         //his function fixes a bug in Navigator's parseInt() function for the Mac.
  477.         //-----------------------------
  478.         //ersion 1.0
  479.         //
  480.         return IsMac?n:parseInt(n);
  481.     }
  482.  
  483.     function HideAll() {
  484.         //IE,NS
  485.         //This function will hide all the currently opened menus.
  486.         //------------------------------
  487.         //Version 1.0
  488.         //
  489.         while(nOM>0)
  490.             Hide();
  491.     }
  492.  
  493.     function GetLeftTop() {
  494.         //IE,NS
  495.         //This function returns the scroll bars position on the menus frame.
  496.         //------------------------------
  497.         //Version 1.0
  498.         //
  499.         if(IE)
  500.             return [mFrame.document.body.scrollLeft,mFrame.document.body.scrollTop];
  501.         if(NS)
  502.             return [mFrame.pageXOffset,mFrame.pageYOffset];
  503.     }
  504.  
  505.     function GetWidthHeight() {
  506.         //IE,NS
  507.         //This function returns the width and height of the menus frame.
  508.         //------------------------------
  509.         //Version 1.0
  510.         //
  511.         if(IE)
  512.             return [mFrame.document.body.clientWidth,mFrame.document.body.clientHeight];
  513.         if(NS)
  514.             return [mFrame.innerWidth,mFrame.innerHeight];
  515.     }
  516.  
  517.     function SetUpEvents() {
  518.         //IE,NS
  519.         //This function initializes the frame variables and setups the event handling.
  520.         //------------------------------
  521.         //Version 1.2
  522.         //
  523.         if(typeof(mFrame)=="undefined")
  524.         //MENUFRAME
  525.         if(typeof(mFrame)=="undefined")
  526.             window.setTimeout("SetUpEvents()",10);
  527.         else {
  528.             if(NS) {
  529.                 mFrame.captureEvents(Event.MOUSEMOVE);
  530.                 mFrame.onmousemove = HideMenus;
  531.                 PrepareEvents();
  532.             }
  533.             mFrame.document.onmousemove = HideMenus;
  534.             document.onmousemove = HideMenus;
  535.         }
  536.     }
  537.  
  538.     function PrepareEvents() {
  539.         //NS
  540.         //This function is called right after the menus are rendered.
  541.         //It has been designed to attach the OnClick event to the <layer> tag. This is being
  542.         //done this way because Navigator does not support a click inline event capturing on
  543.         //the layer tag... duh!
  544.         //------------------------------
  545.         //Version 2.1
  546.         //
  547.         for(var l=0; l<mFrame.document.layers.length; l++) {
  548.             var lo = mFrame.document.layers[l];
  549.             lo.w = lo.clip.width;
  550.             lo.h = lo.clip.height;
  551.             for(var sl=0; sl<lo.layers.length; sl++) {
  552.                 var slo = mFrame.document.layers[l].layers[sl];
  553.                 if(slo.name.indexOf("EH")>0) {
  554.                     slo.document.captureEvents(Event.CLICK);
  555.                     slo.document.onclick = hNSCClick;
  556.                     slo.document.TCode = nTCode[slo.name.split("EH")[1]];
  557.                 }                    
  558.             }
  559.         }
  560.     }
  561.  
  562.     function AutoHide() {
  563.         //IE,NS
  564.         //
  565.         //
  566.         var original_nOM = nOM;
  567.         var OktoClose = true;
  568.         for(;nOM>0;nOM--)
  569.             if(InMenu()) {
  570.                 OktoClose = false;
  571.                 break;
  572.             }
  573.         nOM = original_nOM;
  574.         if(OktoClose)
  575.             HideAll();
  576.         
  577.         TimerHideHnd = window.setTimeout("AutoHide()", TimerHideDelay);
  578.         
  579.     }
  580.  
  581. //BROWSERCODE
  582. //EXPAND
  583.